package com.yylending.las.personal.bandcard;

import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.infrastructure.network.entity.EmptyEntity;
import com.yylending.las.Global;
import com.yylending.las.common.AESEntity;

import java.util.ArrayList;
import java.util.List;

import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

/**
 * Created by HenryZhang on 2016/11/10.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public class BankCardModel extends Model {
	private static final String TAG = "BankCardModel";

	public final static int METHOD_SUPPORT_BANK = 0;
	public final static int METHOD_BIND_BANK_CARD = 1;
	public final static int METHOD_GET_BANKCARD_BACKGROUND = 2; // 后台获取
	public final static int METHOD_GET_BANKCARD_MANUAL = 3; // 手动获取

	@Override
	public Observable getObservable(Object params, int functionalCode) {
		Service service = (Service) NetworkHelper.createService(Service.class);
		switch (functionalCode) {
			case METHOD_SUPPORT_BANK:
				return service.getSupportBankList(Global.LOGIN_TOKEN, new EmptyEntity());
			case METHOD_BIND_BANK_CARD:
				return service.bindBankCard(Global.LOGIN_TOKEN, (AESEntity) params);
			case METHOD_GET_BANKCARD_BACKGROUND:
			case METHOD_GET_BANKCARD_MANUAL:
				return service.getMyBankCard(Global.LOGIN_TOKEN, new EmptyEntity());
		}
		return null;
	}

	@Override
	public void onNext(Object obj) {
		CLog.i(TAG, "onNext: " + obj.toString());
		BaseResultEntity result = cast(obj);
		switch (result.requestCode) {
			case METHOD_SUPPORT_BANK:
				BankListResult bankList = cast(obj);
				EventAgent.post(bankList);
				break;
			case METHOD_BIND_BANK_CARD:
				BindCardResult bindCard = cast(obj);
				EventAgent.post(bindCard);
				break;
			case METHOD_GET_BANKCARD_BACKGROUND:
			case METHOD_GET_BANKCARD_MANUAL:
				MyBankCardResult myBankCard = cast(obj);
				EventAgent.post(myBankCard);
				break;
		}
	}

	private interface Service {
		@POST("server/params/getBankList")
		Observable<BankListResult> getSupportBankList(@Header("token") String token, @Body EmptyEntity notice);

		@POST("server/repay/bindBankCard")
		Observable<BindCardResult> bindBankCard(@Header("token") String token, @Body AESEntity param);

		@POST("server/repay/bindBankList")
		Observable<MyBankCardResult> getMyBankCard(@Header("token") String token, @Body EmptyEntity notice);
	}

	static class BankListResult extends BaseResultEntity<SupportBankList> {
	}

	static class BindCardResult extends BaseResultEntity<BindCardRet> {
	}

	static class BindCardRet{
		String msg = "";
		String userName = "";
	}

	static class MyBankCardResult extends BaseResultEntity<AesJson> {
	}

	public static class AesJson {
		public String json;
	}

	public static class BindCard {
		/**
		 * 客户姓名
		 */
		public String name;
		/**
		 * 银行编号
		 */
		public String bank;
		/**
		 * 银行卡号
		 */
		public String bankNumber;
		/**
		 * 手机号码
		 */
		public String mobile;

		public BindCard(String name, String bank, String bankNumber, String mobile) {
			this.name = name;
			this.bank = bank;
			this.bankNumber = bankNumber;
			this.mobile = mobile;
		}
	}

	public static class SupportBankList{
		List<SupportBank> bankList = new ArrayList<>();

		public static class SupportBank{
			public String bankNo = "";
			public String bankName = "";
		}
	}
}
